<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'base.item.class.php');

class ProductBase extends BaseItemBase {
	
	//constructor
	function ProductBase() {
		parent::BaseItemBase();
		
		$this->item_link = true;
		$this->table = 'pp_products';
		$this->prefix = 'products_';
		
		$this->search_fields = array('name', 'model');
		$this->filter_fields = array('cid:=');
		$this->filter_map = array('cid' => 'cid:=');
	}
	
	//methods
	function onAddItemFilterBind($data_array) {
		//[products_image_banner] => image value
		//[products_image_width_banner] => 200
		//[products_image_height_banner] => 200
		//[products_image_filename_banner] => filename
		//[products_image_filesize_banner] => filesize
		//[products_image_param_banner] => resource/attach id
		
		$fb = array();
		if( isset($data_array['products_image_banner']) && $data_array['products_image_banner'] != '' ) {
			$fb['_banner'] = $data_array['products_image_banner'];
			$fb['_banner_rid'] = $data_array['products_image_param_banner'];
			
			//
			$fb['_images'] = serialize(array(
						'banner' => array(
								'value' => $data_array['products_image_banner'],
								'width' => $data_array['products_image_width_banner'],
								'height' => $data_array['products_image_height_banner'],
								'rid' => $data_array['products_image_param_banner'],
									),
							));
		}
		
		return $fb;
	}
	
	function onUpdateItemFilterBind($items_id, $data_array) {
		//[products_image_banner] => image value
		//[products_image_width_banner] => 200
		//[products_image_height_banner] => 200
		//[products_image_filename_banner] => filename
		//[products_image_filesize_banner] => filesize
		//[products_image_param_banner] => resource/attach id
		
		$fb = array();
		if( isset($data_array['products_image_banner']) && $data_array['products_image_banner'] != '' ) {
			$fb['_banner'] = $data_array['products_image_banner'];
			$fb['_banner_rid'] = $data_array['products_image_param_banner'];
			
			//
			$fb['_images'] = serialize(array(
						'banner' => array(
								'value' => $data_array['products_image_banner'],
								'width' => $data_array['products_image_width_banner'],
								'height' => $data_array['products_image_height_banner'],
								'rid' => $data_array['products_image_param_banner'],
									),
							));
		}
		
		return $fb;
	}
	
	function onFormatItem($item) {
		if( $item['images'] != '' ) {
			$images = unserialize($item['images']);
			if( isset($images['banner']) && is_array($images['banner']) ) {
				$item['banner'] = $images['banner']['value'];
				$item['banner_width'] = $images['banner']['width'];
				$item['banner_height'] = $images['banner']['height'];
				$item['banner_rid'] = $images['banner']['rid'];
			}
		}
		
		return $item;
	}
	
	//
	function onAddImages($data_array, $target_id) {
		if( !is_array($data_array) ) {
			return false;	
		}
		$items_ids = array();
		if( is_array($data_array['products_images']) ) {
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/product/product.image.class.php');
			$mObj = new ProductImageBase();
			
			$cc = 0;
			foreach($data_array['products_images'] as $k => $v) {
				if( strlen($v) > 0 ) {
					$d = array();
					$d['field_target'] = $target_id;
					$d['field_value'] = $v;
					$d['field_width'] = $data_array['products_images_width'][$k];
					$d['field_height'] = $data_array['products_images_height'][$k];
					$d['field_name'] = $data_array['products_images_filename'][$k];
					$d['field_filename'] = $data_array['products_images_filename'][$k];
					
					$items_ids[] = $mObj->addItem($d);
					$cc++;
				}
			}
			
			//
			$count = $mObj->getItemsTotal(array('target:=' => $target_id), -1);
			$this->updateItem($target_id, array('_images' => $count));
		}
		
		return $items_ids;
	}
	
	//
	function onDeleteItemAfter($items_id) {
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/product/product.image.class.php');
		$mObj = new ProductImageBase();
		
		return $mObj->deleteTargetItems($items_id);
	}
	
	//images
	function addImages($data_array, $target_id) {
		if( !is_array($data_array) ) {
			return false;	
		}
		$items_ids = array();
		if( is_array($data_array['field_images']) ) {
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/product/product.image.class.php');
			$mObj = new ProductImageBase();
			
			$cc = 0;
			foreach($data_array['field_images'] as $k => $v) {
				if( strlen($v) > 0 ) {
					$d = array();
					$d['field_target'] = $target_id;
					$d['field_value'] = $v;
					$d['field_width'] = $data_array['field_images_width'][$k];
					$d['field_height'] = $data_array['field_images_height'][$k];
					$d['field_name'] = $data_array['field_images_filename'][$k];
					$d['field_filename'] = $data_array['field_images_filename'][$k];
					
					$items_ids[] = $mObj->addItem($d);
					$cc++;
				}
			}
			
			//
			$count = $mObj->getItemsTotal(array('target:=' => $target_id), -1);
			$this->updateItem($target_id, array('_images' => $count));
		}
		
		return $items_ids;
	}
	
	function getImages($target_id = 0, $filter = '', $check = 0, $limit = true) {
		if( $target_id == 0 ) {
			$target_id = $this->owner;
		}
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/product/product.image.class.php');
		$mObj = new ProductImageBase();
		$mObj->setTargetType($target_id);
		
		return $mObj->getItems($filter, $check, $limit);
	}
	
	function getVideos($target_id = 0, $filter = '', $check = 0, $limit = true) {
		if( $target_id == 0 ) {
			$target_id = $this->owner;
		}
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/product/product.video.class.php');
		$mObj = new ProductVideoBase();
		$mObj->setTargetType($target_id);
		
		return $mObj->getItemsDetail($filter, $check, $limit);
	}
}
//
?>